package com.tchintech.pay.view.certification;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;

import com.tchintech.pay.R;
import com.tchintech.pay.delegate.certification.IdCardFrontDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.model.entity.ResultBean;
import com.tchintech.pay.net.HttpRequester;
import com.tchintech.pay.utils.AppConst;
import com.tchintech.pay.utils.ImageUtils;
import com.tchintech.pay.utils.UserState;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/7 0007.
 */

public class IdCardFrontActivity extends ActivityPresenter<IdCardFrontDelegate> implements View.OnClickListener,UserState {
    private String url_HandPic = "Merchant/upload";
    String saveDir;

    private int RESULT_LOAD_IMAGE = 200;
    private int CAMERA_RESULT = 100;
    private File mPhotoFile;
    private Map<String,String> map;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        viewDelegate.getiv(R.id.identify_iv_example).setImageResource(R.drawable.account_positive_sample);
        viewDelegate.getrela(R.id.identify_rl_example).setBackgroundResource(R.drawable.account_positive);
        viewDelegate.setOnClickListener(this, R.id.identify_bt_confirm, R.id.identify_ib_img);
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };

    }

    @Override
    public void httpCallback(String resultBean, int what) {
        super.httpCallback(resultBean, what);
        switch (what){
            case HttpCode.cardfront:
                //IdCardBackFragment
                System.out.println("数据源1"+resultBean);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.identify_bt_confirm:
                //提交
                if (mPhotoFile != null) {
                    map = new HashMap<>();
                    map.put("state", CARD2);
                    try {
                        httpPost(url_HandPic,map, HttpCode.cardfront,mPhotoFile);
                        viewDelegate.setTexts("请稍等");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    viewDelegate.setTexts("请拍照！！！！");
                }

                break;
            case R.id.identify_ib_img:
                //拿图
                saveDir = Environment.getExternalStorageDirectory().getPath();
                String state = Environment.getExternalStorageState();
                if (state.equals(Environment.MEDIA_MOUNTED)) {
                    mPhotoFile = new File(saveDir, "temp.jpg");
                    mPhotoFile.delete();
                    if (!mPhotoFile.exists()) {
                        try {
                            mPhotoFile.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                            showMsg("照片创建失败!!");
                            return;
                        }
                    }
                    Intent intent = new Intent(
                            "android.media.action.IMAGE_CAPTURE");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(mPhotoFile));
                    startActivityForResult(intent, CAMERA_RESULT);
                } else {
                    showMsg("sdcard无效或没有插入!!!!");
                }
        }
    }
   /* //网路请求界面配置
    private void httpPost(String url, Map<String, String> map, int what,File file ) {
        String path = HttpRequester.path(url, map);
        try {
            showProgressDialog("", "加载中，请稍后...");
            HttpRequester.post(this, what, path, map, this, file);
        } catch (Exception e) {
            hideProgressDialog();
            showMsg("网络异常");
        }
    }*/

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_RESULT && resultCode == RESULT_OK) {
            if (mPhotoFile != null && mPhotoFile.exists()) {
                Bitmap bitmap = ImageUtils.compressImage(mPhotoFile.getPath());
                viewDelegate.getImagebt(R.id.identify_ib_img).setImageBitmap(bitmap);
            }
        }

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = this.getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            mPhotoFile = new File(picturePath);
            cursor.close();
            Bitmap bitmap = ImageUtils.compressImage(picturePath);
            viewDelegate.getImagebt(R.id.identify_ib_img).setImageBitmap(bitmap);

        }
    }

    @Override
    protected Class<IdCardFrontDelegate> getDelegateClass() {
        return IdCardFrontDelegate.class;
    }
}
